//
//  YFEditProfileVC.m
//  project
//
//  Created by Winny Zhang on 2019/3/28.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFEditProfileVC.h"
#import "YFEditProfileContentView.h"
#import "YFProfileTagVC.h"
#import "YFSelectedTagModel.h"
#import "HJSettingVC.h"

@interface YFEditProfileVC ()

/**
 布局TableView
 */
//@property (nonatomic, strong, nonnull) UITableView *tableView;

/**
 内容View
 */
@property (nonatomic, strong, nonnull) YFEditProfileContentView *contentView;

/**
 记录修改过的标签
 再次修改时将传递
 */
@property (nonatomic, strong) NSArray<YFUserInfoTagModel *> *selectedTags;

@property (nonatomic,strong) NSArray <NSString *> * titles;

/**
 跳转class
 */
@property (nonatomic,strong) NSArray <NSString *> * classArray;

@end
static NSString * const identifier1 = @"HJEditProfileCell";
static NSString * const identifier2 = @"SettingCell";

@implementation YFEditProfileVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"个人资料";
//    [self setupViews];
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

-(void)configTableView {
    self.needHeaderRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.contentInset = UIEdgeInsetsMake(4, 0, 0, 0);
}

-(void)loadNetworkData {
    [self.tableView reloadData];
    [self.tableView endRefresh];
}

-(NSArray<NSString *> *)titles {
    if (!_titles) {
        _titles = @[@"头像",@"昵称",@"签名",@"生日",@"职业",@"地址",@"个性标签"];
    }
    return _titles;
}
-(NSArray<NSString *> *)classArray {
    if (!_classArray) {
        _classArray = @[@"HJAvatorVC",@"HJNickNameVC",@"HJSignatureVC",@"HJBirthdayVC",@"HJJobVC",@"HJLocationVC",@"YFProfileTagVC"];
    }
    return _classArray;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
        {
            HJEditProfileCell * editProCell = [tableView cellForRowAtIndexPath:indexPath];
            if (!editProCell) {
                editProCell = [[HJEditProfileCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
            }
            [editProCell loadData:self.titles[indexPath.row]];
            return editProCell;
        }
            break;
        default:
        {
            HJSettingCell * profileCell = [tableView cellForRowAtIndexPath:indexPath];
            if (!profileCell) {
                profileCell = [[HJSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier2];
            }
            [profileCell loadData:self.titles[indexPath.row]];
            return profileCell;
        }
            break;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0:
            return 50;
            break;
        default:
            return 44;
            break;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self ClasFrom:self.classArray[indexPath.row]];
}

//- (void)setupViews {
//    self.view.backgroundColor = [UIColor withRGB:243];
//    [self setupNavigationRightButton];
//    [self setupTableView];
//    [self setupContentView];
//}
//
//- (void)setupTableView {
//    UITableView *tableView = [[UITableView alloc] initWithFrame:SCREEN_BOUNDS style:(UITableViewStylePlain)];
//    tableView.backgroundColor = [UIColor clearColor];
//    tableView.tableFooterView = [UIView new];
//    [self.view insertSubview:tableView atIndex:0];
//    self.tableView = tableView;
//    if (@available(iOS 11.0, *)) {
//        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    } else {
//        self.automaticallyAdjustsScrollViewInsets = NO;
//    }
//    if (IS_FULL_SCREEN) {
//        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 34, 0);
//    } else {
//        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    }
//    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
//}

//- (void)setupNavigationRightButton {
//    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    button.frame = CGRectMake(0, 0, 60, 44);
//    button.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:16];
//    [button setTitleColor:[UIColor colorWithHex:0x666666] forState:(UIControlStateNormal)];
//    [button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
//    [button setTitle:@"保存" forState:(UIControlStateNormal)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
//    @weakify(self)
//    [[button rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        @strongify(self)
//        LKLog(@"保存");
//        // [weakSelf loadRequset];
//        LKLog(@"更新后的参数：%@", self.contentView.params);
//        LKLog(@"用户是否更新了信息：%@", [self.contentView.backupParams isEqual:self.contentView.params] ? @"没有更新" : @"更新了");
//        
//        if (![self.contentView.backupParams isEqual:self.contentView.params]) {
//            [self loadRequsetWithParam:self.contentView.params];
//        }
//    }];
//}

// 编辑资料编辑
//-(void)loadRequsetWithParam:(NSDictionary *)params {
//    
//}
//- (void)setupContentView {
//    YFEditProfileContentView *contentView = [YFEditProfileContentView loadFromXib:[YFEditProfileContentView class]];
//    if (SCREEN_HEIGTH - NAVIGATION_BAR_HEIGHT > 500) {
//        if (IS_FULL_SCREEN) {
//            contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - NAVIGATION_BAR_HEIGHT - 34);
//        } else {
//             contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - NAVIGATION_BAR_HEIGHT);
//        }
//    } else {
//        contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 500);
//    }
//    self.tableView.tableHeaderView = contentView;
//    self.contentView = contentView;
//
//    self.contentView.userInfo = [HJUserManager getUserModel];
//
//    self.selectedTags = [HJUserManager getUserModel].tagModels;
//
//    // 跳转添加标签
//    @weakify(self)
//    self.contentView.selectTagAction = ^{
//        @strongify(self)
//        YFProfileTagVC *vc = [YFProfileTagVC new];
//        vc.defaultTags = self.selectedTags;
//        @weakify(self)
//        vc.selectedCompletion = ^(BOOL isSelected, NSArray<YFUserInfoTagModel *> *selectedTags) {
//            @strongify(self)
//            self.contentView.tags = selectedTags;
//            self.selectedTags = selectedTags;
//        };
//        [self.navigationController pushViewController:vc animated:YES];
//    };
//}

@end


@implementation HJEditProfileCell


-(void)loadData:(NSString *)title {
    self.title.text = title;
    [self.avatorImageView sd_setImageWithURL:[NSURL URLWithString:[HJUserManager getUserModel].avatar]];
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubview];
    }
    return self;
}
-(void)initSubview {
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView * bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    self.bgView = bgView;
    [bgView rounded:UIRectCornerTopLeft | UIRectCornerBottomLeft radius:12];
    [self shadowWithView:bgView];
    [self.contentView addSubview:bgView];
    
    UILabel *title = [UILabel new];
    title.textColor = UIColor.blackColor;
    title.font = [UIFont systemFontOFSize13];
    self.title = title;
    [bgView addSubview:title];
    
    UIImageView * rightRowImageView = [UIImageView new];
    rightRowImageView.image = [UIImage imageNamed:@"ico_mine_right_row"];
    self.rightImageView = rightRowImageView;
    [bgView addSubview:rightRowImageView];
    
    UIImageView * avatorImageView = [UIImageView new];
    self.avatorImageView = avatorImageView;
    [avatorImageView rounded:UIRectCornerAllCorners radius:20 borderColor:[UIColor blackColor] borderWidth:1];
    [bgView addSubview:avatorImageView];
}
- (void)shadowWithView:(UIView *)view {
    dispatch_async(dispatch_get_main_queue(), ^{
        CALayer *layer = [view layer];
        layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.06].CGColor;
        layer.shadowOffset = CGSizeMake(0,2);
        layer.shadowOpacity = 1;
        layer.shadowRadius = 4;
        UIBezierPath  *path = [UIBezierPath bezierPathWithRect:view.bounds];
        layer.shadowPath = path.CGPath;
    });
}
-(void)layoutSubviews {
    [super layoutSubviews];
    
    @weakify(self)
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.top.equalTo(@6);
        make.left.equalTo(self.contentView).with.offset(10);
        make.bottom.equalTo(self.contentView).with.inset(0);
        make.right.equalTo(self.contentView).with.inset(0);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.right.equalTo(self.bgView).with.inset(22);
        make.centerY.mas_equalTo(self.bgView);
        make.size.mas_equalTo(CGSizeMake(self.rightImageView.image.size.width, self.rightImageView.image.size.height));
    }];
    
    [self.avatorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.right.equalTo(self.rightImageView.mas_left).with.inset(10);
        make.centerY.mas_equalTo(self.rightImageView);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.left.equalTo(self.bgView).with.offset(12);
        make.centerY.mas_equalTo(self.bgView);
        make.right.equalTo(self.avatorImageView.mas_left).with.inset(12);
        make.height.mas_equalTo(30);
    }];
}


@end
